package crawler.script;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import crawler.client.sdk.task.java.CrawlerExtractInfo;
import crawler.client.sdk.task.java.CrawlerTaskInfo;
import crawler.utility.http.HttpClientResponse;
import crawler.utility.http.HttpClientUtil;

public class Step3 {
	
	private static Logger log = LoggerFactory.getLogger(Step3.class);
	
	
	public static CrawlerExtractInfo crawler(CrawlerTaskInfo crawlerTaskInfo) throws Exception {
		
		String url =  crawlerTaskInfo.getTaskUrl();
		String preInfo = crawlerTaskInfo.getInformation();
		JSONObject preInfoJSON = JSONObject.parseObject(preInfo);
		
		log.info("Step3  preInfoJSON : " + preInfoJSON);
		
		HttpClientUtil httpClientUtil = new HttpClientUtil();
		httpClientUtil.setUrl(url);
		
		HttpClientResponse responseBean = httpClientUtil.get();
		
		String html = responseBean.getContent();
		Document doc = Jsoup.parse(html);
		Elements itemInfoDiv = doc.select("div#product-intro > div.m-item-inner > div#itemInfo");
		Elements nameElements = itemInfoDiv.select("div#name");
		Elements summaryElements = itemInfoDiv.select("div#summary");
		
		String priceURL = "https://p.3.cn/prices/get?type=1&area=22_1930_50949&pdtk=&pduid=846736818&pdpin=&pdbp=0&skuid=J_SKU&callback=cnp";
				
		List<CrawlerTaskInfo> crawleOutput = new ArrayList<CrawlerTaskInfo>();
		CrawlerTaskInfo crawlerURL;
		
		
		for (int i = 0; i < nameElements.size(); i++) {
			Element summaryElement = summaryElements.get(i);
			Element nameElement = nameElements.get(i);
			
			crawlerURL = new CrawlerTaskInfo();
			
			String name = nameElement.select("h1").first().text();
			String proPrice = "Get Fail";
			
			Element aElement = summaryElement.select("div.dd > a").first();
			if (aElement != null) {
				priceURL = priceURL.replace("SKU", aElement.attr("data-sku"));
			
				httpClientUtil.setUrl(priceURL);
				HttpClientResponse priceResponseBean = httpClientUtil.get();
				
				String priceHtml = priceResponseBean.getContent();
				priceHtml = priceHtml.substring(priceHtml.indexOf("(") + 1,priceHtml.indexOf(")"));
				JSONArray priceJSONArray = JSONArray.parseArray(priceHtml);
				
				proPrice = priceJSONArray.getJSONObject(0).getString("p");
			}

			JSONObject infoObj = new JSONObject(preInfoJSON);
			infoObj.put("nameStep3", name);
			infoObj.put("price", proPrice);
			crawlerURL.setInformation(infoObj.toJSONString());
			
			crawleOutput.add(crawlerURL);
			
			preInfoJSON.put("nameStep3", name);
			preInfoJSON.put("price", proPrice);
		}
		
		return new CrawlerExtractInfo(crawleOutput,preInfoJSON.toJSONString());
	}
	
	
	
	
	public static void main(String[] args) throws Exception {
		String taskUrl = "https://item.jd.com/339997.html";
		
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("nameStep1", "nameStep1");
		jsonObject.put("urlStep1", "urlStep1");
		
		
		CrawlerTaskInfo crawlerTaskInfo = new CrawlerTaskInfo();
		crawlerTaskInfo.setTaskUrl(taskUrl);
		crawlerTaskInfo.setInformation(jsonObject.toJSONString());
		
		CrawlerExtractInfo crawlerExtractInfo = crawler(crawlerTaskInfo);
		System.out.println(crawlerExtractInfo);
	}
	
}
